﻿using System;
using System.Windows.Input;
using QLTT.Commands;

namespace QLTT.ViewModels
{
    /// <summary>
    /// This ViewModelBase subclass requests to be removed 
    /// from the UI when its CloseCommand executes.
    /// This class is abstract.
    /// </summary>
    /// 
    public abstract class WorkspaceViewModel : ViewModelBase
    {
        #region Fields

        DelegateCommand _closeCommand;
        WorkspaceViewModel _parentWorkspace;
        private DelegateCommand _moveNextCommand;
        private DelegateCommand _movePrevCommand;
        private DelegateCommand _moveLastCommand;
        private DelegateCommand _moveFirstCommand;
        private int _curPage = 1;
        private int _numRow = 0;
        #endregion // Fields
        #region Constructor
        protected WorkspaceViewModel()
        {
            Mediator.Register(this);
        }
        #endregion // Constructor
        #region CloseCommand

        /// <summary>
        /// Returns the command that, when invoked, attempts
        /// to remove this workspace from the user interface.
        /// </summary>
        public ICommand CloseCommand
        {
            get
            {
                if (_closeCommand == null)
                    _closeCommand = new DelegateCommand(this.OnRequestClose);

                return _closeCommand;
            }
        }

        #endregion // CloseCommand

        public WorkspaceViewModel ParentWorkspace
        {
            get { return _parentWorkspace; }
            set { _parentWorkspace = value; }
        }
        public int CurPage
        {
            get { return _curPage; }
            set
            {
                if (_curPage == value)
                    return;
                _curPage = value;
                base.OnPropertyChanged("CurPage");
                base.OnPropertyChanged("DisplayString");
            }
        }
        public int NumRow
        {
            get { return _numRow; }
            set
            {
                if (_numRow == value)
                    return;
                _numRow = value;
                base.OnPropertyChanged("NumRow");
                base.OnPropertyChanged("DisplayString");

            }
        }
        public int NumPage
        {
            get { return (_numRow - 1) / RowsPerPage + 1; }
        }
        public string DisplayString
        {
            get{return string.Format("{0}/{1}", CurPage, NumPage);}
        }
        
        #region RequestClose [event]

        /// <summary>
        /// Raised when this workspace should be removed from the UI.
        /// </summary>
        public event EventHandler RequestClose;

        void OnRequestClose()
        {
            EventHandler handler = this.RequestClose;
            if (handler != null)
                handler(this, EventArgs.Empty);
        }

        #endregion // RequestClose [event]
        public ICommand MoveNextCommand
        {
            get
            {
                if (_moveNextCommand == null)
                {
                    _moveNextCommand = new DelegateCommand(MoveNext, CanMoveNext);
                }
                return _moveNextCommand;
            }
        }
        public ICommand MovePrevCommand
        {
            get
            {
                if (_movePrevCommand == null)
                {
                    _movePrevCommand = new DelegateCommand(MovePrev, CanMovePrev);
                }
                return _movePrevCommand;
            }
        }
        public ICommand MoveLastCommand
        {
            get
            {
                if (_moveLastCommand == null)
                {
                    _moveLastCommand = new DelegateCommand(MoveLast, CanMoveLast);
                }
                return _moveLastCommand;
            }
        }
        public ICommand MoveFirstCommand
        {
            get
            {
                if (_moveFirstCommand == null)
                {
                    _moveFirstCommand = new DelegateCommand(MoveFirst, CanMoveFirst);
                }
                return _moveFirstCommand;
            }
        }
        private void MoveNext()
        {
            CurPage++;
            UpdateList();
        }
        private bool CanMoveNext()
        {
            return CurPage < NumPage;
        }
        private void MovePrev()
        {
            CurPage--;
            UpdateList();
        }
        private bool CanMovePrev()
        {
            return CurPage > 1;
        }
        private void MoveLast()
        {
            CurPage = NumPage;
            UpdateList();
        }
        private bool CanMoveLast()
        {
            return CurPage < NumPage;
        }
        private void MoveFirst()
        {
            CurPage = 1;
            UpdateList();
        }
        private bool CanMoveFirst()
        {
            return CurPage > 1;
        }
        public virtual void UpdateList()
        {
            return;
        }
    }
}
